package com.wg.web_app.controller;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.wg.web_app.service.NumberService;
import com.wg.web_app.service.UserService;


@SpringBootApplication
@Controller
public class UserController {

	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public String login(@RequestParam("loginname")  String username, @RequestParam("loginpass") String userpass) {
		UserService user = new UserService();
		return new Gson().toJson(user.login(username, userpass));
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public String register(@RequestParam("username") String username, @RequestParam("loginname") String loginname,
			@RequestParam("loginpass") String loginpass) {
		UserService user = new UserService();
		return new Gson().toJson(user.register(loginname, loginpass, username));
	}
	
	@RequestMapping(value = "/querynumbers", method = RequestMethod.GET)
	@ResponseBody
	public String querynumbers(@RequestParam("userid") int userid) {
		UserService user = new UserService();
		return new Gson().toJson(user.getNumbers(userid));
	}
	
	@RequestMapping(value = "/getgroups", method = RequestMethod.GET)
	@ResponseBody
	public String getGroups(@RequestParam("userid") int userid) {
		UserService user = new UserService();
		return new Gson().toJson(user.queryGroups(userid));
	}
	
	@RequestMapping(value = "/addnumber", method = RequestMethod.POST)
	@ResponseBody
	public String addnumber(@RequestParam("userid") int userid, @RequestParam("detail") String detail,
			@RequestParam("number") String number) {
		NumberService user = new NumberService();
		return new Gson().toJson(user.add(userid,detail,number));
	}
}
